/*
 * @Author: your name
 * @Date: 2021-08-24 00:00:05
 * @LastEditTime: 2022-02-12 22:12:38
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \phone\src\confirmation_win.cpp
 */
#include "confirmation_win.h"
#include "interface.h"
ConfirmationPage::ConfirmationPage()
{
    /**
     * @brief
     * 默认是提示框
     */
    type = ConfirmationType::TYPE_TIPS;
    keepTime = 0;
    confirmEvent = nullptr;
    ctx = nullptr;
    onlyConfirm = true;
}
void ConfirmationPage::OpenConfirmationPage(ConfirmationPage* page)
{
    Context* p = new Context;
    p->ctx = page;
    auto funCallback = [](void* ctx) mutable {
        IS_VALID_PTR(ctx)
        Context* p2 = (Context*)ctx;
        KeyEventConfirmationWin event;
        ConfirmationPage* window = (ConfirmationPage*)p2->ctx;
        if (!window->PageOpen(CONFIRMATION_WIN_NAME)) {
            LOG_E("window open error\n");
            delete window;
            delete p2;
            return;
        }
        window->SetKeyEvent(event);
        window->SaveContext(p2);
        window->StartDisplay();
        Interface::GetInstance().SetCurrentPage(window);
    };
    p->eventCallback = funCallback;
    if (!GuiEngine::GetInstance().IdleQueue(p)) {
        LOG_W("IdleQueue_V2 falied\n");
        delete p;
    }
}
void ConfirmationPage::StartDisplay()
{
    std::unique_ptr<Widget> view(GuiEngine::GetInstance().NewWidget());
    if (GuiEngine::GetInstance().Lookup(GetThisPage(), "title", true, view.get())) {
        GuiEngine::GetInstance().SetTrText(view.get(), typeTrText[static_cast<int>(type)].c_str());
    }
    if (GuiEngine::GetInstance().Lookup(GetThisPage(), "tips", true, view.get())) {
        /**
         * @brief
         * 优先显示译文，次优先级显示字符串。
         */
        if (trText.size() > 0) {
            GuiEngine::GetInstance().SetTrText(view.get(), trText.c_str());
        } else {
            GuiEngine::GetInstance().SetTextUtf8(view.get(), tips.c_str());
        }
    }
    if (GuiEngine::GetInstance().Lookup(GetThisPage(), "confirm", true, view.get())) {
        /**
         * @brief
         * 重置确认按钮译文。
         */
        if (confirmTrText.size() > 0) {
            GuiEngine::GetInstance().SetTrText(view.get(), confirmTrText.c_str());
        }
    }
    if (!onlyConfirm && GuiEngine::GetInstance().Lookup(GetThisPage(), "cancel", true, view.get())) {
        GuiEngine::GetInstance().SetTrText(view.get(), "cancel");
    }
    /**
     * @brief Set the Only Confirm object
     * 根据onlyConfirm参数外部设置重置一下键值。
     */
    SetOnlyConfirm(onlyConfirm);
}
void ConfirmationPage::Soft1()
{
    CommonFunc::GetInstance().CloseCurrentPage();
    if (confirmEvent) {
        confirmEvent(false, ctx);
    }
}
void ConfirmationPage::Soft4()
{
    CommonFunc::GetInstance().CloseCurrentPage();
    if (confirmEvent) {
        confirmEvent(true, ctx);
    }
}
void ConfirmationPage::SetTips(std::string value)
{
    tips = value;
}
void ConfirmationPage::SetKeepTime(unsigned int time)
{
    keepTime = time;
}
void ConfirmationPage::SetTrText(std::string textName)
{
    trText = textName;
}
void ConfirmationPage::SetConfirmtaionType(ConfirmationType type)
{
    ConfirmationPage::type = type;
}
void ConfirmationPage::SetConfirmCallback(void (*event)(bool, void*), void* ctx)
{
    confirmEvent = event;
    ConfirmationPage::ctx = ctx;
}
void ConfirmationPage::SetOnlyConfirm(bool onlyConfirm)
{
    ConfirmationPage::onlyConfirm = onlyConfirm;
    if (!ConfirmationPage::onlyConfirm) {
        KeyEventRedefine(KeyboardValue::KEY_SOFT_KEY_1, KeyCmd::KEY_SOFT_KEY_1);
    }
}
void ConfirmationPage::SetConfirmTrText(std::string trText)
{
    confirmTrText = trText;
}